﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace XnaGuiContentPipeline
{
    [ContentTypeWriter]
    public class TiledRectangleContentWriter : ContentTypeWriter<TiledRectangleContent>
    {
        protected override void Write(ContentWriter output, TiledRectangleContent value)
        {
            output.WriteObject<Rectangle>(value.rectangle1);
            output.WriteObject<Rectangle>(value.rectangle2);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "CellarDoor.Gui.Theming.TiledRectangleReader, CellarDoor.Gui";
        }
    }

    [ContentTypeWriter]
    public class ThemeSpriteContentWriter : ContentTypeWriter<ThemeSpriteContent>
    {
        protected override void Write(ContentWriter output, ThemeSpriteContent value)
        {
            output.Write(value.Name);
            output.Write(value.Resolution);
            output.WriteObject<TiledRectangleContent>(value.Rectangle);
            output.WriteSharedResource<Texture2DContent>(value.TextureContent);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "CellarDoor.Gui.Theming.UiThemeSpriteReader, CellarDoor.Gui";
        }
    }

    [ContentTypeWriter]
    public class ThemeElementContentWriter : ContentTypeWriter<ThemeElementContent>
    {
        protected override void Write(ContentWriter output, ThemeElementContent value)
        {
            output.Write(value.Name);
            output.Write(value.Resolution);
            output.Write(value.States.Count);
            foreach (var item in value.States)
            {
                output.WriteObject<ThemeSpriteContent>(item);
            }
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "CellarDoor.Gui.Theming.UiThemeElemetReader, CellarDoor.Gui";
        }
    }

    [ContentTypeWriter]
    public class ThemeFontContentWriter : ContentTypeWriter<ThemeFontContent>
    {
        protected override void Write(ContentWriter output, ThemeFontContent value)
        {
            output.Write(value.Name);
            output.Write(value.Resolution);
            output.WriteExternalReference<SpriteFontContent>(value.Font);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "CellarDoor.Gui.Theming.UiThemeFontReader, CellarDoor.Gui";
        }
    }

    [ContentTypeWriter]
    public class UiThemeContentWriter : ContentTypeWriter<UiThemeContent>
    {
        protected override void Write(ContentWriter output, UiThemeContent value)
        {
            output.Write(value.Fonts.Count);
            foreach (var item in value.Fonts)
            {
                output.WriteObject<ThemeFontContent>(item);
            }

            output.Write(value.Sprites.Count);
            foreach (var item in value.Sprites)
            {
                output.WriteObject<ThemeSpriteContent>(item.Value);
            }

            output.Write(value.Elements.Count);
            foreach (var item in value.Elements)
            {
                output.WriteObject<ThemeElementContent>(item);
            }
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "CellarDoor.Gui.Theming.UiThemeReader, CellarDoor.Gui";
        }
    }
}
